#ifndef CANON_HH
#define CANON_HH

#include <stdio.h>  // FILE
#include <vector>

#include "zuc/nml_intf/zucpos.h"
#include "zuctool.h"
#include "nml_intf/canon_position.hh"

/*
  This is the header file that all applications that use the
  canonical commands for three- to nine-axis machining should include.

  Three mutually orthogonal (in a right-handed system) X, Y, and Z axes
  are always present. In addition, there may be zero to three rotational
  axes: A (parallel to the X-axis), B (parallel to the Y-axis), and C
  (parallel to the Z-axis). Additionally there may be zero to three linear
  axes: U, V and W.

  In the functions that use rotational axes, the axis value is that of a
  wrapped linear axis, in degrees.

  It is assumed in these activities that the spindle tip is always at
  some location called the 'current location,' and the controller always
  knows where that is. It is also assumed that there is always a
  'selected plane' which must be the XY-plane, the YZ-plane, or the
  ZX-plane of the machine.
*/

#define OFF 0
#define ON 1

typedef struct
{ /* type for NURBS control points */
    double X, Y, W;
} CONTROL_POINT;

typedef struct
{
    double X, Y;
} PLANE_POINT;

typedef int CANON_PLANE;
#define CANON_PLANE_XY 1
#define CANON_PLANE_YZ 2
#define CANON_PLANE_XZ 3
#define CANON_PLANE_UV 4
#define CANON_PLANE_VW 5
#define CANON_PLANE_UW 6

typedef int CANON_UNITS;
#define CANON_UNITS_INCHES 1
#define CANON_UNITS_MM 2
#define CANON_UNITS_CM 3

typedef int CANON_MOTION_MODE;
#define CANON_EXACT_STOP 1
#define CANON_EXACT_PATH 2
#define CANON_CONTINUOUS 3

typedef int CANON_SPEED_FEED_MODE;
#define CANON_SYNCHED 1
#define CANON_INDEPENDENT 2

typedef int CANON_DIRECTION;
#define CANON_STOPPED 1
#define CANON_CLOCKWISE 2
#define CANON_COUNTERCLOCKWISE 3

typedef int CANON_FEED_REFERENCE;
#define CANON_WORKPIECE 1
#define CANON_XYZ 2

typedef int CANON_SIDE;
#define CANON_SIDE_RIGHT 1
#define CANON_SIDE_LEFT 2
#define CANON_SIDE_OFF 3

typedef int CANON_AXIS;
#define CANON_AXIS_X 1
#define CANON_AXIS_Y 2
#define CANON_AXIS_Z 3
#define CANON_AXIS_A 4
#define CANON_AXIS_B 5
#define CANON_AXIS_C 6
#define CANON_AXIS_U 7
#define CANON_AXIS_V 8
#define CANON_AXIS_W 9

/* Currently using the typedefs above rather than the enums below
typedef enum {CANON_PLANE_XY, CANON_PLANE_YZ, CANON_PLANE_XZ} CANON_PLANE;
typedef enum {CANON_UNITS_INCHES, CANON_UNITS_MM, CANON_UNITS_CM} CANON_UNITS;
typedef enum {CANON_EXACT_STOP, CANON_EXACT_PATH, CANON_CONTINUOUS}
             CANON_MOTION_MODE;
typedef enum {CANON_SYNCHED, CANON_INDEPENDENT} CANON_SPEED_FEED_MODE;
typedef enum {CANON_STOPPED, CANON_CLOCKWISE, CANON_COUNTERCLOCKWISE}
             CANON_DIRECTION;
typedef enum {CANON_WORKPIECE, CANON_XYZ} CANON_FEED_REFERENCE;
typedef enum {CANON_SIDE_RIGHT, CANON_SIDE_LEFT, CANON_SIDE_OFF} CANON_SIDE;
typedef enum {CANON_AXIS_X, CANON_AXIS_Y, CANON_AXIS_Z, CANON_AXIS_A,
              CANON_AXIS_B, CANON_AXIS_C} CANON_AXIS;
*/

//added by alex.li at 2018.09.04
typedef enum
{
    USING_X = 0,
    USING_A,
    USING_B,
    USING_C
} UsingAxis;

struct CANON_VECTOR
{
    CANON_VECTOR() {}
    CANON_VECTOR(double _x, double _y, double _z)
    {
        x = _x;
        y = _y;
        z = _z;
    }
    double x, y, z;
};

typedef struct CanonConfig_t
{
    CanonConfig_t() : rotary_unlock_for_traverse(-1) {}

    int rotary_unlock_for_traverse;  // jointnumber or -1

    // these are both always positive
    double css_maximum;
    double css_numerator;

    int feed_mode;
    int synched;

    /*
  canonEndPoint is the last programmed end point, stored in case it's
  needed for subsequent calculations. It's in absolute frame, mm units.

  note that when segments are queued for the naive cam detector that the
  canonEndPoint may not be the last programmed endpoint.  get_last_pos()
  retrieves the xyz position after the last of the queued segments.  these
  are also in absolute frame, mm units.
  */
    CANON_POSITION endPoint;
    CANON_UNITS lengthUnits;
    CANON_PLANE activePlane;
    /* Tool length offset is saved here */
    ZucPose toolOffset;
    /* motion control mode is used to signify blended v. stop-at-end moves.
   Set to 0 (invalid) at start, so first call will send command out */
    CANON_MOTION_MODE motionMode;
    /* motion path-following tolerance is used to set the max path-following
   deviation during CANON_CONTINUOUS.
   If this param is 0, then it will behave as zuc always did, allowing
   almost any deviation trying to keep speed up. */
    double motionTolerance;
    double naivecamTolerance;
    /* Spindle speed is saved here */
    double spindleSpeed;
    int spindle_dir;
    /* Prepped tool is saved here */
    //   int preppedTool;
    /*
  Feed rate is saved here; values are in mm/sec or deg/sec.
  It will be initially set in INIT_CANON() below.
*/
    double linearFeedRate;
    double angularFeedRate;
    /* optional program stop */
    bool optional_program_stop;
    /* optional block delete */
    bool block_delete;
    /* Used to indicate whether the current move is linear, angular, or 
   a combination of both. */
    //AJ says: linear means axes XYZ move (lines or even circles)
    //         angular means axes ABC move
    int cartesian_move;
    int angular_move;
} CanonConfig_t;

/* Initialization */

/* reads world model data into the canonical interface */
extern void INIT_CANON();

/* Offset the origin to the point with absolute coordinates x, y, z,
a, b, and c. Values of x, y, z, a, b, and c are real numbers. The units
are whatever length units are being used at the time this command is
given. */

extern void CANON_UPDATE_END_POINT(double x, double y, double z, double a, double b, double c, double u, double v, double w);
/* Called from zuctask to update the canon position during skipping through
   programs started with start-from-line > 0. */

extern void USE_LENGTH_UNITS(CANON_UNITS u);

/* Use the specified units for length. Conceptually, the units must
be either inches or millimeters. */

extern void SELECT_PLANE(CANON_PLANE pl);

/* Use the plane designated by selected_plane as the selected plane.
Conceptually, the selected_plane must be the XY-plane, the XZ-plane, or
the YZ-plane. */

/* Free Space Motion */

extern void SET_TRAVERSE_RATE(double rate);

/* Set the traverse rate that will be used when the spindle traverses. It
is expected that no cutting will occur while a traverse move is being
made. */

extern void STRAIGHT_TRAVERSE(int lineno, double x, double y, double z, double a, double b, double c, double u, double v, double w);

extern void MOVL_TRAVERSE(int lineno,
                          int set_planner_type,
                          int planner_type,
                          double x,
                          double y,
                          double z,
                          double a,
                          double b,
                          double c,
                          double u,
                          double v,
                          double w,
                          double vel,
                          double acceleration,
                          double jerk,
                          double ori_velocity,
                          double ori_acceleration,
                          int di_type,
                          int di_index,
                          int di_state,
                          int robot_id = 0);
extern void MOVC_TRAVERSE(int lineno,
                          int set_planner_type,
                          int planner_type,
                          double* x,
                          double* y,
                          double* z,
                          double* a,
                          double* b,
                          double* c,
                          double vel,
                          double acceleration,
                          double jerk,
                          double count,
                          int di_type,
                          int di_index,
                          int di_state,
                          int circlemode);
extern int GET_MOVC_END(ZucPose* start, ZucPose const* const mid, ZucPose const* const end, double count, ZucPose* out);

extern void JOINT_TRAVERSE(int lineno,
                           int set_planner_type,
                           int planner_type,
                           double* jointPos,
                           double* currentJointPos,
                           double vel,
                           double acceleration,
                           double jerk,
                           int di_type,
                           int di_index,
                           int di_state,
                           ZucPose* toolOffset,
                           ZucPose* userOffset,
                           int robot_id = 0);
extern void MOVS_TRAVERSE(int lineno,
                          const double* jointPos,
                          const double* a0,
                          const double* a1,
                          const double* a2,
                          const double* a3,
                          double time,
                          const ZucPose* toolOffset,
                          const ZucPose* userOffset);

/*

Move at traverse rate so that at any time during the move, all axes
have covered the same proportion of their required motion. The final
XYZ position is given by x, y, and z. If there is an a-axis, its final
position is given by a_position, and similarly for the b-axis and c-axis.
A more positive value of a rotational axis is in the counterclockwise
direction.

Clockwise or counterclockwise is from the point of view of the
workpiece. If the workpiece is fastened to a turntable, the turntable
will turn clockwise (from the point of view of the machinist or anyone
else not moving with respect to the machining center) in order to make
the tool move counterclockwise from the point of view of the
workpiece.

*/

/* Machining Attributes */

extern void SET_FEED_RATE(double rate);

/*

SET_FEED_RATE sets the feed rate that will be used when the spindle is
told to move at the currently set feed rate. The rate is either:
1. the rate of motion of the tool tip in the workpiece coordinate system,
   which is used when the feed_reference mode is "CANON_WORKPIECE", or
2. the rate of motion of the tool tip in the XYZ axis system, ignoring
   motion of other axes, which is used when the feed_reference mode is
   "CANON_XYZ".

The units of the rate are:

1. If the feed_reference mode is CANON_WORKPIECE:
length units (inches or millimeters according to the setting of
CANON_UNITS) per minute along the programmed path as seen by the
workpiece.

2. If the feed_reference mode is CANON_XYZ:
A. For motion including one rotational axis only: degrees per minute.
B. For motion including two rotational axes only: degrees per minute
   In this case, the rate applies to the axis with the larger angle
   to cover, and the second rotational axis rotates so that it has
   always completed the same proportion of its required motion as has
   the rotational axis to which the feed rate applies.
C. For motion involving one or more of the XYZ axes (with or without
   simultaneous rotational axis motion): length units (inches or
   millimeters according to the setting of CANON_UNITS) per minute
   along the programmed XYZ path.

*/

extern void SET_FEED_REFERENCE(CANON_FEED_REFERENCE reference);

/*

This sets the feed_reference mode to either CANON_WORKPIECE or
CANON_XYZ.

The CANON_WORKPIECE mode is more natural and general, since the rate
at which the tool passes through the material must be controlled for
safe and effective machining. For machines with more than the three
standard XYZ axes, however, computing the feed rate may be
time-consuming because the trajectories that result from motion in
four or more axes may be complex. Computation of path lengths when
only XYZ motion is considered is quite simple for the two standard
motion types (straight lines and helical arcs).

Some programming languages (rs274kt, in particular) use CANON_XYZ
mode. In these languages, the task of dealing with the rate at which
the tool tip passes through material is pushed back on the NC-program
generator, where the computation of path lengths is (almost always in
1995) an off-line activity where speed of calculation is not critical.

In CANON_WORKPIECE mode, some motions cannot be carried out as fast as
the programmed feed rate would require because axis motions tend to
cancel each other. For example, an arc in the YZ-plane can exactly
cancel a rotation around the A-axis, so that the location of the tool
tip with respect to the workpiece does not change at all during the
motion; in this case, the motion should take no time, which is
impossible at any finite rate of axis motion. In such cases, the axes
should be moved as fast as possible consistent with accurate
machining.

It would be possible to omit the SET_FEED_REFERENCE command from the
canonical commands and operate always in one mode or the other,
letting the interpreter issue SET_FEED_RATE commands, if necessary to
compensate if the NC language being interpreted used the other mode.

This would create two disadvantages when the feed_reference mode
assumed by the canonical commands differed from that assumed by the NC
language being interpreted:

1. The output code could have a lot of SET_FEED_RATE commands not
found in the input code; this is a relatively minor consideration.

2. If the interpreter reads a program in language which uses the
CANON_XYZ mode and writes canonical commands in the CANON_WORKPIECE
mode, both the interpreter and the executor of the output canonical
commands would have to perform a lot of complex calculations. With the
SET_FEED_REFERENCE command available, both do only simple calculations
for the same motions.

*/

extern void SET_FEED_MODE(int mode);

/* This sets the feed mode: 0 for feed in units per minute, and 1 for feed in
 * units per revolution.  In units per revolution mode, the values are in
 * inches per revolution (G20 in effect) or mm per minute (G21 in effect) */

extern void SET_MOTION_CONTROL_MODE(CANON_MOTION_MODE mode, double tolerance);

extern void SET_NAIVECAM_TOLERANCE(double tolerance);

/*

This sets the motion control mode to one of: CANON_EXACT_STOP,
CANON_EXACT_PATH, or CANON_CONTINUOUS.

For CANON_CONTINUOUS another parameter defines the maximum path deviation. 
If tolerance=0 then any path deviation may occur, speed is maximized.

*/

extern void SET_CUTTER_RADIUS_COMPENSATION(double radius);

/* Set the radius to use when performing cutter radius compensation. */

extern void START_CUTTER_RADIUS_COMPENSATION(int direction);

/* Conceptually, the direction must be left (meaning the cutter
stays to the left of the programmed path) or right. */

extern void STOP_CUTTER_RADIUS_COMPENSATION();

/* Do not apply cutter radius compensation when executing spindle
translation commands. */

/* used for threading */
extern void START_SPEED_FEED_SYNCH(double feed_per_revolution, bool velocity_mode);
extern void STOP_SPEED_FEED_SYNCH();

/* Machining Functions */

extern void ARC_FEED(int lineno,
                     double first_end,
                     double second_end,
                     double first_axis,
                     double second_axis,
                     int rotation,
                     double axis_end_point,
                     double a,
                     double b,
                     double c,
                     double u,
                     double v,
                     double w);

/* Move in a helical arc from the current location at the existing feed
rate. The axis of the helix is parallel to the x, y, or z axis,
according to which one is perpendicular to the selected plane. The
helical arc may degenerate to a circular arc if there is no motion
parallel to the axis of the helix.

1. If the selected plane is the xy-plane:
A. first_end is the x-coordinate of the end of the arc.
B. second_end is the y-coordinate of the end of the arc.
C. first_axis is the x-coordinate of the axis (center) of the arc.
D. second_axis is the y-coordinate of the axis.
E. axis_end_point is the z-coordinate of the end of the arc.

2. If the selected plane is the yz-plane:
A. first_end is the y-coordinate of the end of the arc.
B. second_end is the z-coordinate of the end of the arc.
C. first_axis is the y-coordinate of the axis (center) of the arc.
D. second_axis is the z-coordinate of the axis.
E. axis_end_point is the x-coordinate of the end of the arc.

3. If the selected plane is the zx-plane:
A. first_end is the z-coordinate of the end of the arc.
B. second_end is the x-coordinate of the end of the arc.
C. first_axis is the z-coordinate of the axis (center) of the arc.
D. second_axis is the x-coordinate of the axis.
E. axis_end_point is the y-coordinate of the end of the arc.

If rotation is positive, the arc is traversed counterclockwise as
viewed from the positive end of the coordinate axis perpendicular to
the currently selected plane. If rotation is negative, the arc is
traversed clockwise. If rotation is 0, first_end and second_end must
be the same as the corresponding coordinates of the current point and
no arc is made (but there may be translation parallel to the axis
perpendicular to the selected plane and motion along the rotational axes).
If rotation is 1, more than 0 but not more than 360 degrees of arc
should be made. In general, if rotation is n, the amount of rotation
in the arc should be more than ([n-1] x 360) but not more than (n x
360).

The radius of the helix is determined by the distance from the current
location to the axis of helix or by the distance from the end location
to the axis of the helix. It is recommended that the executing system
verify that the two radii are the same (within some tolerance) at the
beginning of executing this function.

While the XYZ motion is going on, move the rotational axes so that
they have always covered the same proportion of their total motion as
a point moving along the arc has of its total motion.

*/

extern void STRAIGHT_FEED(int lineno, double x, double y, double z, double a, double b, double c, double u, double v, double w);

/* Additional functions needed to calculate nurbs points */

extern std::vector<unsigned int> knot_vector_creator(unsigned int n, unsigned int k);
extern double Nmix(unsigned int i, unsigned int k, double u, std::vector<unsigned int> knot_vector);
extern double Rden(double u, unsigned int k, std::vector<CONTROL_POINT> nurbs_control_points, std::vector<unsigned int> knot_vector);
extern PLANE_POINT nurbs_point(double u, unsigned int k, std::vector<CONTROL_POINT> nurbs_control_points, std::vector<unsigned int> knot_vector);
extern PLANE_POINT nurbs_tangent(double u, unsigned int k, std::vector<CONTROL_POINT> nurbs_control_points, std::vector<unsigned int> knot_vector);
extern double alpha_finder(double dx, double dy);

/* Canon calls */

extern void NURBS_FEED(int lineno, std::vector<CONTROL_POINT> nurbs_control_points, unsigned int k);
/* Move at the feed rate along an approximation of a NURBS with a variable number
 * of control points
 */

/* Move at existing feed rate so that at any time during the move,
all axes have covered the same proportion of their required motion.
The meanings of the parameters is the same as for STRAIGHT_TRAVERSE.*/

extern void RIGID_TAP(int lineno, double x, double y, double z);

/* Perform a probing operation. This is a temporary addition to the
canonical machining functions and its semantics are not defined.
When the operation is finished, all axes should be back where they
started. */

extern void STOP();

/* stop motion after current feed */

extern void DWELL(double seconds, int line_num, short sub_thread);

extern void SETTOOL(int line_num, double* toolOffset, double* currJntPos, const ZucPose* currUserFrame);

extern void SETUSERFRAME(int line_num, double* userFrame, double* currJntPos, const ZucPose* currToolOffset);

extern void ENABLE_ADMIT(int enable);

/* Tool Functions */
extern void SET_TOOL_TABLE_ENTRY(int pocket, int toolno, ZucPose offset, double diameter, double frontangle, double backangle, int orientation);

/* Miscellaneous Functions */

extern void CLAMP_AXIS(CANON_AXIS axis);

/* Clamp the given axis. If the machining center does not have a clamp
for that axis, this command should result in an error condition in the
controller.

An attempt to move an axis while it is clamped should result in an
error condition in the controller. */

extern void COMMENT(const char* s);

/* This function has no physical effect. If commands are being printed or
logged, the comment command is printed or logged, including the string
which is the value of comment_text. This serves to allow formal
comments at specific locations in programs or command files. */

/* used for EDM adaptive moves with motion internal feed override (0..1) */
extern void DISABLE_ADAPTIVE_FEED();
extern void SET_TOOL_ID(int lineno, int id);
extern void SET_USER_ID(int lineno, int id);
extern int GET_TOOL_OFFSET(int id, ZucPose* tool);
extern int GET_USER_OFFSET(int id, ZucPose* user);
extern int GET_TASK_EXECSTATE();

extern void SET_PAYLOAD(int line_num, Payload payload);
extern void SET_CLSN_LEVEL(int line_num, int clsnLevel);
extern void SET_ADMITCTRLCONFIG(int line_num, int axis, FtConfig ftConfig, int now);
extern void SET_ENDFORCECOND(int line_num, int axis, EndForceCond endForceCond);
extern void SET_COMPLIANTCON(int lineno, double* compliantCondition);
extern void SET_COMPLIANTTYPE(int lineno, int* typeEna);
extern void SET_VELCOMPLIANTLEVEL(int lineno, double* velCompliantCtrl);
extern void SET_FORCRCTRLFRAME(int lineno, int ftFrame);
extern void DISABLE_FORCECONTROL(int lineno, int disableForceCtrl);
extern void CONVEYOR_LINEAR_ENABLE(double x, double y, double z, double pulseEquivalent, double maxDistance);
extern void CONVEYOR_CIRCULAR_ENABLE(double p1x,
                                     double p1y,
                                     double p1z,
                                     double p2x,
                                     double p2y,
                                     double p2z,
                                     double p3x,
                                     double p3y,
                                     double p3z,
                                     double pulseEquivalent,
                                     int rotate_tool,
                                     double maxDistance);
extern void CONVEYOR_DISABLE(void);
extern void TOPPRA_FINISH_LEFT_TC(void);
extern void TOPPRA_FIRST_COMMAND(void);

/* used to deactivate user control of feed override */
extern void DISABLE_FEED_OVERRIDE();
extern void ENABLE_FEED_OVERRIDE();
extern void TIO_UPDATE_SIGNAL(const char* sigName, float freq, short sub_thread);
extern void TIO_SEND_COMMAND(int chnId, unsigned char* byteArr, int len, int crcType, short sub_thread);

extern void MESSAGE(char* s);
extern void LOG(char* s);
extern void LOGOPEN(char* s);
extern void LOGAPPEND(char* s);
extern void LOGCLOSE();

extern void PALLET_SHUTTLE();

/* If the machining center has a pallet shuttle mechanism (a mechanism
which switches the position of two pallets), this command should cause
that switch to be made. If either or both of the pallets are missing,
this will not result in an error condition in the controller.

If the machining center does not have a pallet shuttle, this command
should result in an error condition in the controller. */

extern void UNCLAMP_AXIS(CANON_AXIS axis);

/* Unclamp the given axis. If the machining center does not have a clamp
for that axis, this command should result in an error condition in the
controller. */

/* NURB Functions */
extern void NURB_KNOT_VECTOR(); /* double knot values, -1.0 signals done */
extern void NURB_CONTROL_POINT(int i, double x, double y, double z, double w);
extern void NURB_FEED(double sStart, double sEnd);

/* Block delete */
extern void SET_BLOCK_DELETE(bool enabled);
/* Command to set the internal reference of block delete.
The ON value for enabled will cause the interpreter to discard lines
that start with the "/" character. */

extern bool GET_BLOCK_DELETE(void);
/* Command to get the internal reference of optional block delete. */

/* Program Functions */
extern void OPTIONAL_PROGRAM_STOP();
/* If the machining center has an optional stop switch, and it is on
when this command is read from a program, stop executing the program
at this point, but be prepared to resume with the next line of the
program. If the machining center does not have an optional stop
switch, or commands are being executed with a stop after each one
already (such as when the interpreter is being used with keyboard
input), this command has no effect. */

extern void SET_OPTIONAL_PROGRAM_STOP(bool state);
/* Command to set the internal reference of optional program stop.
Any non-zero value for state will cause the execution to stop on
optional stops. */

extern bool GET_OPTIONAL_PROGRAM_STOP();
/* Command to get the internal reference of optional program stop. */
void PROGRAM_END(int threadId = 0);
// extern void PROGRAM_END();
/* If a program is being read, stop executing the program and be prepared
to accept a new program or to be shut down. */

extern void PROGRAM_STOP(int threadId = 0);
// extern void PROGRAM_STOP();
extern void PROGRAM_RESUME(int threadId = 0);
/* If this command is read from a program, stop executing the program at
this point, but be prepared to resume with the next line of the
program. If commands are being executed with a stop after each one
already (such as when the interpreter is being used with keyboard
input), this command has no effect. */

// extern void PROGRAM_EXIT();

/* Commands to set/reset output bits and analog values */
extern void SET_MOTION_OUTPUT_BIT(int line_num, int ioType, int index, short sub_thread);  // 0-->basic IO, 1-->tool IO
extern void CLEAR_MOTION_OUTPUT_BIT(int line_num, int ioType, int index, short sub_thread);
extern void SET_AUX_OUTPUT_BIT(int line_num, int ioType, int index, short sub_thread);
extern void CLEAR_AUX_OUTPUT_BIT(int line_num, int ioType, int index, short sub_thread);

extern void SET_MOTION_OUTPUT_VALUE(int line_num, int ioType, int index, double value, short sub_thread);
extern void SET_AUX_OUTPUT_VALUE(int line_num, int ioType, int index, double value, short sub_thread);

extern void update_linenum(int line_num, short sub_thread);

/* Commands to wait for, query input bits and analog values */

#define ANALOG_INPUT 10
#define DIGITAL_INPUT 0
#define TIO_DIGITAL_INPUT 1
#define EXTIO_DIGITAL_INPUT 2
#define MB_SLAVE_DIGITAL_INPUT 4
#define PN_DIGITAL_INPUT 5
#define EIP_DIGITAL_INPUT 6

#define WAIT_MODE_IMMEDIATE 0
#define WAIT_MODE_RISE 1
#define WAIT_MODE_FALL 2
#define WAIT_MODE_HIGH 3
#define WAIT_MODE_LOW 4

extern int WAIT(int line_num,   /* -1 by default*/
                int index,      /* index of the motion exported input */
                int input_type, /* 0=DIGITAL_INPUT 1=TIO_DIGITAL_INPUT, 10=ANALOG_INPUT */
                int wait_type,  /* 0 - immediate, 1 - rise, 2 - fall, 3 - be high, 4 - be low */
                double timeout, /* time to wait [in seconds], if the input didn't change the value -1 is returned */
                short sub_thread);
/* WAIT - program execution is stopped until the input selected by index 
   changed to the needed state (specified by wait_type).
   Return value: either wait_type if timeout didn't occur, or -1 otherwise. */

/* tell canon the next move needs the rotary to be unlocked */
extern int UNLOCK_ROTARY(int line_no, int joint_num);

/* tell canon that is no longer the case */
extern int LOCK_ROTARY(int line_no, int joint_num);

/*************************************************************************/

/* Canonical "Give me information" functions for the interpreter to call

In general, returned values are valid only if any canonical do it commands
that may have been called for have been executed to completion. If a function
returns a valid value regardless of execution, that is noted in the comments
below.

*/

/* The interpreter is not using this function
// Returns the system angular unit factor, in units / degree
extern double GET_EXTERNAL_ANGLE_UNIT_FACTOR();
*/

// Returns the system feed rate
extern double GET_EXTERNAL_FEED_RATE();

/* The interpreter is not using this function
// Returns the system length unit factor, in units / mm
extern double GET_EXTERNAL_LENGTH_UNIT_FACTOR();
*/

// Returns the system length unit type
CANON_UNITS GET_EXTERNAL_LENGTH_UNIT_TYPE();

extern double GET_EXTERNAL_LENGTH_UNITS();
extern double GET_EXTERNAL_ANGLE_UNITS();

// Returns the current motion control mode
extern CANON_MOTION_MODE GET_EXTERNAL_MOTION_CONTROL_MODE();

// Returns the current motion path-following tolerance
extern double GET_EXTERNAL_MOTION_CONTROL_TOLERANCE();

/* The interpreter is not using these six GET_EXTERNAL_ORIGIN functions

// returns the current a-axis origin offset
extern double GET_EXTERNAL_ORIGIN_A();

// returns the current b-axis origin offset
extern double GET_EXTERNAL_ORIGIN_B();

// returns the current c-axis origin offset
extern double GET_EXTERNAL_ORIGIN_C();

// returns the current x-axis origin offset
extern double GET_EXTERNAL_ORIGIN_X();

// returns the current y-axis origin offset
extern double GET_EXTERNAL_ORIGIN_Y();

// returns the current z-axis origin offset
extern double GET_EXTERNAL_ORIGIN_Z();

*/

// returns nothing but copies the name of the parameter file into
// the filename array, stopping at max_size if the name is longer
// An empty string may be placed in filename.
extern void GET_EXTERNAL_PARAMETER_FILE_NAME(char* filename, int max_size);

// returns the currently active plane
extern CANON_PLANE GET_EXTERNAL_PLANE();

// returns the current a-axis position
extern double GET_EXTERNAL_POSITION_A();

// returns the current b-axis position
extern double GET_EXTERNAL_POSITION_B();

// returns the current c-axis position
extern double GET_EXTERNAL_POSITION_C();

// returns the current x-axis position
extern double GET_EXTERNAL_POSITION_X();

// returns the current y-axis position
extern double GET_EXTERNAL_POSITION_Y();

// returns the current z-axis position
extern double GET_EXTERNAL_POSITION_Z();

// returns the current u-axis position
extern double GET_EXTERNAL_POSITION_U();

// returns the current v-axis position
extern double GET_EXTERNAL_POSITION_V();

// returns the current w-axis position
extern double GET_EXTERNAL_POSITION_W();

// calculate the forward kinematics
extern int GET_KINEMATICS_FORWARD(const double* joint,
                                  ZucPose* world,
                                  const unsigned long* fflags,
                                  unsigned long* iflags,
                                  const ZucPose* toolOffset,
                                  const ZucPose* usrOffset);
extern int GET_KINEMATICS_INVERSE(const ZucPose* world,
                                  double* joint,
                                  const unsigned long* iflags,
                                  unsigned long* fflags,
                                  const ZucPose* toolOffset,
                                  const ZucPose* usrOffset);

extern int GET_KINEMATICS_FORWARD_TOOL(const double* joint, ZucPose* world, const ZucPose* toolOffset, const ZucPose* usrOffset);
extern int GET_KINEMATICS_INVERSE_TOOL(const ZucPose* world, double* joint, const ZucPose* toolOffset, const ZucPose* usrOffset);
extern int GET_TOOL_MOVE_POS(const ZucPose* world, const ZucPose* pos, ZucPose* out, const ZucPose* toolOffset, const ZucPose* usrOffset);

// get current joint position
extern int GET_CURRENT_JOINT_POSITION(double* joint);

// Returns zero if queue is not empty, non-zero if the queue is empty
// This always returns a valid value
extern int GET_EXTERNAL_QUEUE_EMPTY();

// returns current robot tool length offset
extern double GET_EXTERNAL_ROBOT_TOOL_LENGTH_XOFFSET();
extern double GET_EXTERNAL_ROBOT_TOOL_LENGTH_YOFFSET();
extern double GET_EXTERNAL_ROBOT_TOOL_LENGTH_ZOFFSET();
extern double GET_EXTERNAL_ROBOT_TOOL_LENGTH_AOFFSET();
extern double GET_EXTERNAL_ROBOT_TOOL_LENGTH_BOFFSET();
extern double GET_EXTERNAL_ROBOT_TOOL_LENGTH_COFFSET();
extern double GET_EXTERNAL_ROBOT_TOOL_LENGTH_UOFFSET();
extern double GET_EXTERNAL_ROBOT_TOOL_LENGTH_VOFFSET();
extern double GET_EXTERNAL_ROBOT_TOOL_LENGTH_WOFFSET();

// Returns number of slots in carousel
extern int GET_EXTERNAL_POCKETS_MAX();

// Returns the CANON_TOOL_TABLE structure associated with the tool
// in the given pocket
extern CANON_TOOL_TABLE GET_EXTERNAL_TOOL_TABLE(int pocket);

// Returns the system traverse rate
extern double GET_EXTERNAL_TRAVERSE_RATE();

// Returns the enabled/disabled status for feed override, spindle
// override, adaptive feed, and feed hold
extern int GET_EXTERNAL_FEED_OVERRIDE_ENABLE();

// Functions to query digital/analog Inputs
/* def is a default value which should be returned by canon functions
   that can't actually read external hardware - simulators and such */
extern int GET_EXTERNAL_DIGITAL_INPUT(int index, int def);
/* returns current value of the digital input selected by index.*/

extern double GET_EXTERNAL_ANALOG_INPUT(int index, double def);
/* returns current value of the analog input selected by index.*/

// Returns the mask of axes present in the system
extern int GET_EXTERNAL_AXIS_MASK();

extern FILE* _outfile;              /* where to print, set in main */
extern CANON_TOOL_TABLE _tools[];   /* in canon.cc */
extern int _pockets_max;            /* in canon.cc */
extern char _parameter_file_name[]; /* in canon.cc */
#define PARAMETER_FILE_NAME_LENGTH 100

#define USER_DEFINED_FUNCTION_NUM 100
typedef void (*USER_DEFINED_FUNCTION_TYPE)(int num, double arg1, double arg2);
extern USER_DEFINED_FUNCTION_TYPE USER_DEFINED_FUNCTION[USER_DEFINED_FUNCTION_NUM];
extern int USER_DEFINED_FUNCTION_ADD(USER_DEFINED_FUNCTION_TYPE func, int num);

/* to be called by zucTaskPlanExecute when done interpreting.  This causes the
 * last segment to be output, if it has been held to do segment merging */
extern void FINISH(void);

// expose CANON_ERROR
extern void CANON_ERROR(const char* fmt, ...) __attribute__((format(printf, 1, 2)));

// queue a call to a task-time Python plugin method
// call is expected to be a tuple of (method,pickled posargs,pickled kwargs)
extern void PLUGIN_CALL(int len, const char* call);

// same for IoTask context
extern void IO_PLUGIN_CALL(int len, const char* call);

#endif /* ifndef CANON_HH */
